using UnityEngine;
using System.Collections;

/// \class  gkPowerComponent
/// \brief  Manage a power value
[AddComponentMenu("GK/Engine/Game/Power/gkPowerComponent")]
public class gkPowerComponent : MonoBehaviour
{
	/// Max power
	public float PowerMax = 1.0f;
	
	/// The power
	private float m_fPower;
	
	/// The event component
	private gkEventComponent m_rEventComponent;
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		// Grab the event component
		m_rEventComponent = GetComponent<gkEventComponent>();
	}
	
	/// \brief  Get the power
	public float GetPower()
	{
		return m_fPower;
	}
	
	/// \brief  Get the power
	public float GetPowerPercentage()
	{
		return m_fPower/PowerMax;
	}
	
	/// \brief  Use the power
	public void UsePower(float a_fPowerUsage)
	{
		m_fPower -= a_fPowerUsage;
		// Notify of the power change
		m_rEventComponent.RaiseEvent(gkPowerEvent.PowerChange);
		if(m_fPower < 0.0f)
		{
			m_fPower = 0.0f;
		}
	}
	
	/// \brief  Use the power
	public void AddPower(float a_fPowerGain)
	{
		m_fPower += a_fPowerGain;
		// Notify of the power change
		m_rEventComponent.RaiseEvent(gkPowerEvent.PowerChange);
		if(m_fPower > PowerMax)
		{
			m_fPower = PowerMax;
		}
	}
	
	/// \brief  Nullify the power
	public void Reset()
	{
		if(m_fPower > 0.0f)
		{
			// Notify of the power change
			m_rEventComponent.RaiseEvent(gkPowerEvent.PowerChange);
		}
		m_fPower = 0.0f;
	}
}